package com.relsell.view;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import com.relsell.admin.AboutUs;
import com.relsell.pointofsales.R;


public class NormalUser extends FragmentActivity {

	private TabHost mTabHost;
	private ViewPager mViewPager;
	private TabsAdapter mTabsAdapter;
    private View tabOne;
    private View tabTwo;
    private View tabBarOne;
    private View tabBarTwo;
	@Override
	public void onBackPressed() {
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
				NormalUser.this);
 
			alertDialogBuilder.setTitle(R.string.exit_dialog_title);
			alertDialogBuilder
				.setMessage(R.string.exit_dialog_txt)
				.setCancelable(true)
				.setPositiveButton(R.string.exit_txt,new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog,int id) {
						
							NormalUser.this.finish();
							NormalUser.super.onBackPressed();
					
					}
				  })
				.setNegativeButton(R.string.switch_user_txt,new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog,int id) {
						dialog.cancel();
						Intent intent = new Intent(NormalUser.this, Login.class);
						startActivity(intent);
						NormalUser.this.finish();
					}
				});
			
				AlertDialog alertDialog = alertDialogBuilder.create();
				alertDialog.show();
				
	}


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.fragmentadapter);

		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup();

		mViewPager = (ViewPager) findViewById(R.id.pager);
		mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);

		LayoutInflater li = this.getLayoutInflater();
		
		tabOne = li.inflate(R.layout.custom_tab,null);
		TextView t =(TextView)tabOne.findViewById(R.id.tabsText);
		t.setText("Submit Sales");
		tabBarOne = (View)tabOne.findViewById(R.id.view1);
		tabBarOne.setSelected(true);
		mTabsAdapter.addTab(
				mTabHost.newTabSpec("one").setIndicator(tabOne),
				SubmitSales.class, null);
		
		tabTwo = li.inflate(R.layout.custom_tab,null);
		
		TextView t1 =(TextView)tabTwo.findViewById(R.id.tabsText);
		t1.setText("Edit Profile");
		tabBarTwo = (View)tabTwo.findViewById(R.id.view1);
		tabBarTwo.setSelected(false);
		
		mTabsAdapter.addTab(
				mTabHost.newTabSpec("two").setIndicator(tabTwo),
				EditProfile.class, null);

		if (savedInstanceState != null) {
			mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
		}

	}

	public static class TabsAdapter extends FragmentPagerAdapter implements
			TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
		private final Context mContext;
		private final TabHost mTabHost;
		private final ViewPager mViewPager;
		private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

		static final class TabInfo {
			private final String tag;
			private final Class<?> clss;
			private final Bundle args;

			TabInfo(String _tag, Class<?> _class, Bundle _args) {
				tag = _tag;
				clss = _class;
				args = _args;
			}
		}

		static class DummyTabFactory implements TabHost.TabContentFactory {
			private final Context mContext;

			public DummyTabFactory(Context context) {
				mContext = context;
			}

			@Override
			public View createTabContent(String tag) {
				View v = new View(mContext);
				v.setMinimumWidth(0);
				v.setMinimumHeight(0);
				return v;
			}
		}

		public TabsAdapter(FragmentActivity activity, TabHost tabHost,
				ViewPager pager) {
			super(activity.getSupportFragmentManager());
			mContext = activity;
			mTabHost = tabHost;
			mViewPager = pager;
			mTabHost.setOnTabChangedListener(this);
			mViewPager.setAdapter(this);
			mViewPager.setOnPageChangeListener(this);
		}

		public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
			tabSpec.setContent(new DummyTabFactory(mContext));
			String tag = tabSpec.getTag();

			TabInfo info = new TabInfo(tag, clss, args);
			mTabs.add(info);
			mTabHost.addTab(tabSpec);
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			return mTabs.size();
		}

		@Override
		public Fragment getItem(int position) {
			TabInfo info = mTabs.get(position);

			return Fragment.instantiate(mContext, info.clss.getName(),
					info.args);

		}

		@Override
		public void onTabChanged(String tabId) {
			int position = mTabHost.getCurrentTab();
			mViewPager.setCurrentItem(position);
			// mTabHost.getTabWidget().getChildTabViewAt(position).setBackgroundColor(R.color.black);
		}

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
		}

		@Override
		public void onPageSelected(int position) {
			// Unfortunately when TabHost changes the current tab, it kindly
			// also takes care of putting focus on it when not in touch mode.
			// The jerk.
			// This hack tries to prevent this from pulling focus out of our
			// ViewPager.
			TabWidget widget = mTabHost.getTabWidget();
			int oldFocusability = widget.getDescendantFocusability();
			widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
			// widget.setBackgroundColor(color.transparent);
			// mTabHost.setBackgroundColor(color.black);
			mTabHost.setCurrentTab(position);
			widget.setDescendantFocusability(oldFocusability);
		}

		@Override
		public void onPageScrollStateChanged(int state) {
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		 getMenuInflater().inflate(R.menu.menu, menu);//Menu Resource, Menu  
	        return true;
		
	}
	
	 @Override  
	    public boolean onOptionsItemSelected(MenuItem item) {  
	        switch (item.getItemId()) {  
	        
	        case R.id.menu_logout:
				Intent intent = new Intent(NormalUser.this, Login.class);
				startActivity(intent);
				finish();
				return true;
				
	            case R.id.menu_register:  
	            	 this.setProgressBarIndeterminateVisibility(true);
					 new Register().execute();
					 this.setProgressBarIndeterminateVisibility(false);
	              Toast.makeText(getApplicationContext(),"Successfully Registered",Toast.LENGTH_LONG).show();  
	            return true;     
	  
	            case R.id.menu_aboutus:  
	            	Intent in = new Intent(NormalUser.this, AboutUs.class);
	    			startActivity(in);
	              return true; 
	  
	              default:  
	                return super.onOptionsItemSelected(item);  
	        }  
	    }  
}
